﻿using BgWorks.Models;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace MyApp.WebApi.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UpLoadController : ControllerBase
    {
       private string fullpath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExtDll");
        public UpLoadController()
        {

        }
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResponseApi<List<fileInfo>> GetFile()
        {
            ResponseApi<List<fileInfo>> res = new ResponseApi<List<fileInfo>> { };
            List<fileInfo> filelist = new List<fileInfo>();
            
            DirectoryInfo Dir = new DirectoryInfo(fullpath);
            List<FileInfo> files = Dir.GetFiles("*.*")?.OrderBy(c => c.CreationTime)?.ToList();
            for (int i = 0; i < files.Count; i++)
            {
                filelist.Add(new fileInfo() { name = files[i].Name, url = files[i].FullName });
            }
            res.data = filelist;
            res.code = 200;
            return res;
        }

        /// <summary>
        /// 删除一个文件
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        [HttpPost]

        public ResponseApi<string> DeleteFile([FromBody] fileInfo fileInfo)
        {
            ResponseApi<string> res = new ResponseApi<string>() { };
            try
            {
               string filename= Path.Combine(fullpath, fileInfo.name);
                FileInfo info = new FileInfo(filename);
                if (info.Exists)
                {
                    info.Delete();
                    res.code = 200;
                }
                else
                    res.msg = "文件不存在";
            }
            catch (Exception ex)
            {
                res.msg = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 本地上传图片信息至服务器处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResponseApi<string> SaveFile()
        {
            ResponseApi<string> res = new ResponseApi<string>() { };
            if (Request.Form.Files == null)
            {
                return res;
            }
            foreach (FormFile file in Request.Form.Files)
            {
                #region 文件校验
                string exten = Path.GetExtension(file.FileName).ToLower();
                //if (exten != ".jpg" && exten != ".png" && exten != ".wav") continue;
                long size = file.Length / 1024; //文件大小KB
                //if (size > 1024)
                //{
                //    res.ErrMsg = "存在过大文件！";
                //    continue;
                //}
                #endregion                                                                   
               
                string fullName = Path.Combine(fullpath, file.FileName);
                using (FileStream stream = new FileStream(fullName, FileMode.Create, FileAccess.Write))
                {
                    file.CopyTo(stream);
                    stream.Flush();
                }

            }
            res.code = 200;
            return res;
        }


        // FileReportDto 对象
        public class fileInfo
        {
            public string name { get; set; }

            public string url { get; set; }
        }
    }
}

